
#include "EZLog.h"
#include <stdio.h>
#include <string>

static bool s_isDebug = true;

void EZLogSetRunTimeDebug(bool isDebug)
{
	s_isDebug = isDebug;
}
bool EZLogIsRunTimeDebug()
{
	return s_isDebug;
}

#ifdef __IOS__

void commonLog(const char *file,const int line,int priority,const char *tag,const int guessLen,const char *format,...)
{
	if(EZLogIsRunTimeDebug()==false) {
		return;
	}
    std::string fileStr(file);
	int filenameIndex = (int)fileStr.find_last_of('/');
	std::string fileName;
	if(filenameIndex>=0 && filenameIndex < fileStr.length() ){
		fileName = fileStr.substr(filenameIndex+1,fileStr.length()-filenameIndex-1);
	}else{
		fileName = fileStr;
	}
    
    char cstr[guessLen];
	memset(cstr,0,guessLen);
    
	va_list arg_ptr;
	va_start(arg_ptr, format);
	vsprintf(cstr,format,arg_ptr);
	va_end(arg_ptr);
    
    printf("%s::%d::%s:%s\n",fileName.c_str(),line,tag,cstr);
}

void commonlogToFile(const char *file,const int line,int priority,const char *tag,const char *format,...)
{

}
#else

#include <jni.h>
void commonLog(const char *file,const int line,int priority,const char *tag,const int guessLen,const char *format,...)
{
	if(EZLogIsRunTimeDebug()==false){
		return;
	}
    android_LogPriority priority_android = (android_LogPriority)priority;
	std::string fileStr(file);
	int filenameIndex = (int)fileStr.find_last_of('/');
	std::string fileName;
	if(filenameIndex>=0  && filenameIndex < fileStr.length() ){
		fileName = fileStr.substr(filenameIndex+1,fileStr.length()-filenameIndex-1);
	}else{
		fileName = fileStr;
	}

	char cstr[guessLen];
	memset(cstr,0,guessLen);


	va_list arg_ptr;
	va_start(arg_ptr, format);
	vsprintf(cstr,format,arg_ptr);
	va_end(arg_ptr);

	__android_log_print(priority_android,tag,"%s::%d::%s",fileName.c_str(),line,cstr);

#ifdef _DEBUG_FILE_LOG_AUTO_
	commonlogToFile(file,line,priority,tag,format);
#endif
}
static int logFileisInit = 0;
static void logFileInit()
{
	if(logFileisInit==1){
		return;
	}
	logFileisInit = 1;
	FILE *f = fopen("/sdcard/kugou/log/libaudioplayer.log","w");
	fclose(f);
}
#include "GlobalTools.h"
void commonlogToFile(const char *file,const int line,int priority,const char *tag,const int guessLen,const char *format,...)
{
	if (EZLogIsRunTimeDebug() == false) {
		return;
	}
	logFileInit();
	android_LogPriority priority_android = (android_LogPriority)priority;
		std::string fileStr(file);
		int filenameIndex = (int)fileStr.find_last_of('/');
		std::string fileName;
		if(filenameIndex>=0  && filenameIndex < fileStr.length() ){
			fileName = fileStr.substr(filenameIndex+1,fileStr.length()-filenameIndex-1);
		}else{
			fileName = fileStr;
		}

		char cstr[guessLen];
		memset(cstr,0,guessLen);

		sprintf(cstr,"%s::%d::",fileName.c_str(),line);
		int len = strlen(cstr);

		va_list arg_ptr;
		va_start(arg_ptr, format);
		vsprintf(cstr+len,format,arg_ptr);
		va_end(arg_ptr);
		len = strlen(cstr);
		memset(cstr+len,'\n',1);
		len +=1;
		FILE *f = fopen("/sdcard/kugou/log/libaudioplayer.log","a+");
		if(f){
			fwrite(cstr,len,1,f);
			fclose(f);
		}

}

#endif
